home *** CD-ROM | disk | FTP | other *** search
/ PC World Komputer 2010 April / PCWorld0410.iso / pluginy Firefox / 2410 / 2410.xpi / chrome / content / foxmarks-progress.js < prev    next >
Text File  |  2010-01-28  |  6KB  |  184 lines

  1. /* 
  2.  Copyright 2005-2008 Foxmarks Inc.
  3.  
  4.  foxmarks-progress.js: handles the UI and top-level logic for
  5.  merge, sync, upload, and download operations.
  6.   
  7.  */
  8.  
  9. var gCancelled;
  10. var gQuick;
  11. var gSyncTypes = ["bookmarks", "passwords"];
  12.  
  13. var newObserver = {
  14.     _document: null,
  15.     _window: null,
  16.  
  17.     observe: function(subject, topic, data) {
  18.         var result = eval(data);
  19.         //Xmarks.LogWrite("Progress: " + data);
  20.         if (this._document) {
  21.             if(result.status == 3){
  22.                 var label = this._document.getElementById(result.component);
  23.                 if(label){
  24.                     label.setAttribute("value",  result.phase == "end" ?
  25.                         Xmarks.Bundle().GetStringFromName("progress.sync.done") :
  26.                         Xmarks.Bundle().GetStringFromName("progress.sync.working")
  27.                     );
  28.                 }
  29.                 var image =this._document.getElementById(result.component + "-check");
  30.                 if(image){
  31.                     image.setAttribute("src", result.phase == "end" ?
  32.                         "chrome://foxmarks/skin/images/progress-good.png":
  33.                         "chrome://foxmarks/skin/images/wheel.gif"
  34.                     );
  35.                 }
  36.             }
  37.             else if (result.status != 1) { // complete?
  38.                 this._window.arguments[1].status = result.status;
  39.                 this._window.arguments[1].msg = result.msg;
  40.                 this._window.arguments[1].result = result;
  41.                 if (gQuick) {
  42.                     setTimeout(this._window.close, 1000);
  43.                 } else {
  44.                     gCancelled = true;
  45.                     setTimeout(this._window.close, 100);
  46.                 }
  47.             }
  48.         }        
  49.         try {
  50.             this._window.sizeToContent();
  51.         } catch(e) {}
  52.     },
  53.  
  54.     adjustDialogButtons: function(status) {
  55.         var d = this._document.documentElement;
  56.         var help = d.getButton("help");
  57.         help.hidden = (status >= 0 && status <= 2);
  58.         var cancel = d.getButton("cancel");
  59.         cancel.hidden = (status != 1);
  60.         try {
  61.             this._window.sizeToContent();
  62.         } catch(e) {}
  63.     }
  64. };
  65.  
  66. var gFoxmarksService = null;
  67. var Cc = Components.classes;
  68. var Ci = Components.interfaces;
  69.  
  70. function onProgressLoad() {
  71.     gCancelled = false;
  72.     gQuick = false;
  73.  
  74.     // get reference to foxmarks-service to process the request
  75.     gFoxmarksService = Cc["@foxcloud.com/extensions/foxmarks;1"].
  76.         getService(Ci.nsIFoxmarksService);
  77.  
  78.     var os = Cc["@mozilla.org/observer-service;1"].
  79.         getService(Ci.nsIObserverService);
  80.     newObserver._document = document;
  81.     newObserver._window = window;
  82.     os.addObserver(newObserver, "foxmarks-service", false);
  83.     
  84.     var okay = false;
  85.  
  86.     switch (window.arguments[0]) {
  87.         case "upload":
  88.             gQuick = false;
  89.             okay = gFoxmarksService.upload();
  90.             break;
  91.         
  92.         case "restore":
  93.             gQuick = false;
  94.             okay = gFoxmarksService.restore(window.arguments[2]);
  95.             break;
  96.             
  97.         case "download":
  98.             gQuick = false;
  99.             okay = gFoxmarksService.download();
  100.             break;
  101.         
  102.         case "deletepasswords":
  103.             gQuick = false;
  104.             document.getElementById("lineitems").setAttribute("hidden", "true");
  105.             okay = gFoxmarksService.purgepasswords();
  106.             break;
  107.         case "synch":
  108.             gQuick = false;
  109.             okay = gFoxmarksService.synchronize();
  110.             break;
  111.  
  112.         case "status":
  113.             gQuick = true;
  114.             okay = gFoxmarksService.status();
  115.             break;
  116.             
  117.         case "initialSync":
  118.             var ca = window.arguments[2];
  119.             gQuick = true;
  120.             okay = gFoxmarksService.synchronizeInitial(ca.remoteIsMaster, 
  121.                 ca.merge);
  122.             break;
  123.     }
  124.  
  125.     var len = gSyncTypes.length;
  126.     var x;
  127.     for(x = 0; x < len; x++){
  128.         var type = gSyncTypes[x];
  129.         var label = document.getElementById(type);
  130.         label.setAttribute("value", Xmarks.gSettings.isSyncEnabled(type) ?
  131.             Xmarks.Bundle().GetStringFromName("progress.sync.enabled") :
  132.             Xmarks.Bundle().GetStringFromName("progress.sync.disabled")
  133.         );
  134.     }
  135.  
  136.     if (!okay) {
  137.         // Service is busy. Disconnect the observer and simulate a
  138.         // "busy" status message.
  139.         onProgressUnload();
  140.         newObserver.observe(null, null,
  141.             { msg: Xmarks.Bundle().GetStringFromName("msg.busy"), status: 4 }.
  142.             toSource());
  143.     }
  144. }
  145.  
  146. function onProgressUnload() {
  147.     var os = Cc["@mozilla.org/observer-service;1"].
  148.         getService(Ci.nsIObserverService);
  149.     try {
  150.         os.removeObserver(newObserver, "foxmarks-service");
  151.     } catch (e) {
  152.         Xmarks.LogWrite("Warning: removeObserver failed.");
  153.     }
  154. }
  155.  
  156. function onProgressCancel() {
  157.     gFoxmarksService.cancel();
  158.   
  159.     // the first time through, just let callbacks from the cancellation
  160.     // requests above shut us down
  161.     // if we somehow got stuck, allow a second press of the cancel
  162.     // button to actually shut the dialog box down.
  163.     
  164.     if (gCancelled) {
  165.         window.arguments[1].status = -1;
  166.         window.arguments[1].msg = Xmarks.Bundle().GetStringFromName("msg.cancelled");
  167.         return true;
  168.     } else {
  169.         gCancelled = true;
  170.         return false;   // let the callbacks close us down
  171.     }
  172. }
  173.  
  174. function onProgressHelp() {
  175.     var element = document.getElementById("status");
  176.     if (element) {
  177.         var errmsg = element.value.replace(/ /g, "_");
  178.         window.arguments[1].helpurl =
  179.             Xmarks.Bundle().formatStringFromName("url.error", [errmsg], 1);
  180.         setTimeout(window.close, 100);
  181.     }
  182.     return false;
  183. }
  184.